package com.xingongchang.baolebei.activity;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.external.maxwin.view.XListView.IXListViewListener;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.MyDialog;
import com.umeng.analytics.MobclickAgent;
import com.xingongchang.baolebei.BaolebeiManager;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.adapter.HistoryAdapter;
import com.xingongchang.baolebei.model.OrderModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.protocol.GOODORDER;
import com.xingongchang.baolebei.protocol.ORDER_GOODS_LIST;

public class HistoryActivity extends AlipayActivity implements
		BusinessResponse, IXListViewListener {

	private String flag;
	private TextView title;
	private ImageView back;
	private XListView xlistView;
	private HistoryAdapter tradeAdapter;

	private View null_paView;

	private ArrayList<String> items = new ArrayList<String>();

	private OrderModel orderModel;

	public Handler messageHandler;
	private MyDialog mDialog;
	int id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Resources resource = (Resources) getBaseContext().getResources();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history);

		Intent intent = getIntent();
		flag = intent.getStringExtra("flag");

		title = (TextView) findViewById(R.id.top_view_text);

		back = (ImageView) findViewById(R.id.top_view_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		null_paView = findViewById(R.id.null_pager);
		xlistView = (XListView) findViewById(R.id.trade_list);
		xlistView.setPullLoadEnable(true);
		xlistView.setRefreshTime();
		xlistView.setXListViewListener(this, 1);
		orderModel = new OrderModel(this);
		orderModel.addResponseListener(this);

		String awa = resource.getString(R.string.await_pay);
		String ship = resource.getString(R.string.await_ship);
		String shipped = resource.getString(R.string.shipped);
		String fin = resource.getString(R.string.profile_history);

		if (flag.equals("await_pay")) {
			title.setText(awa);
			
			/**
			 * 在这里请求数据
			 */
			orderModel.getOrder("await_pay");
		} else if (flag.equals("await_ship")) {
			title.setText(ship);
			/**
			 * 在这里请求数据
			 */
			orderModel.getOrder("await_ship");

		} else if (flag.equals("shipped")) {
			title.setText(shipped);
			/**
			 * 在这里请求数据
			 */
			orderModel.getOrder("shipped");

		} else if (flag.equals("finished")) {
			title.setText(fin);
			/**
			 * 在这里请求数据
			 */
			orderModel.getOrder("finished");
		}

		messageHandler = new Handler() {

			public void handleMessage(final Message msg) {

				if (msg.what == 1) {
					GOODORDER order = (GOODORDER) msg.obj;
					order_info = order.order_info;

					if (BaolebeiManager
							.getAlipayCallback(getApplicationContext()) != null
							&& BaolebeiManager
									.getAlipayParterId(getApplicationContext()) != null
							&& BaolebeiManager
									.getAlipaySellerId(getApplicationContext()) != null
							&& BaolebeiManager
									.getRsaAlipayPublic(getApplicationContext()) != null
							&& BaolebeiManager
									.getRsaPrivate(getApplicationContext()) != null) {
						if (0 == order_info.pay_code.compareTo("alipay")) {
							performPay();
						} else {
							orderModel.orderPay(order_info.order_id);
						}
					} else {
						orderModel.orderPay(order_info.order_id);
					}
				} else if (msg.what == 2) {
					Resources resource = (Resources) getBaseContext()
							.getResources();
					String exit = resource
							.getString(R.string.balance_cancel_or_not);
					String exiten = resource.getString(R.string.prompt);
					final MyDialog cancelOrders = new MyDialog(
							HistoryActivity.this, exiten, exit);
					cancelOrders.show();
					cancelOrders.positive
							.setOnClickListener(new OnClickListener() {
								@Override
								public void onClick(View v) {
									cancelOrders.dismiss();
									GOODORDER order = (GOODORDER) msg.obj;
									order_info = order.order_info;
									orderModel.orderCancle(order_info.order_id);

								}
							});
					cancelOrders.negative
							.setOnClickListener(new OnClickListener() {
								@Override
								public void onClick(View v) {
									cancelOrders.dismiss();
								}
							});

				}

				else if (msg.what == 3) {
					GOODORDER order = (GOODORDER) msg.obj;
					order_info = order.order_info;
					orderModel.affirmReceived(order_info.order_id);
				}
			}
		};

	}

	public void setOrder() {

		Resources resource = (Resources) getBaseContext().getResources();
		String nodata = resource.getString(R.string.no_data);
		if (orderModel.order_list.size() == 0) {
			null_paView.setVisibility(View.VISIBLE);
			xlistView.setVisibility(View.GONE);
		} else {
			null_paView.setVisibility(View.GONE);
			xlistView.setVisibility(View.VISIBLE);
		}

		if (flag.equals("await_pay")) {

			if (tradeAdapter == null) {
				tradeAdapter = new HistoryAdapter(this, orderModel.order_list,
						1);
				xlistView.setAdapter(tradeAdapter);
			} else {
				tradeAdapter.list = orderModel.order_list;
				tradeAdapter.notifyDataSetChanged();
			}
			tradeAdapter.parentHandler = messageHandler;

		} else if (flag.equals("await_ship")) {
			if (tradeAdapter == null) {
				tradeAdapter = new HistoryAdapter(this, orderModel.order_list,
						2);
				xlistView.setAdapter(tradeAdapter);
			} else {
				tradeAdapter.list = orderModel.order_list;
				tradeAdapter.notifyDataSetChanged();
			}
			tradeAdapter.parentHandler = messageHandler;

		} else if (flag.equals("shipped")) {
			if (tradeAdapter == null) {
				tradeAdapter = new HistoryAdapter(this, orderModel.order_list,
						3);
				xlistView.setAdapter(tradeAdapter);
			} else {
				tradeAdapter.list = orderModel.order_list;
				tradeAdapter.notifyDataSetChanged();
			}

			tradeAdapter.parentHandler = messageHandler;

		} else if (flag.equals("finished")) {

			if (tradeAdapter == null) {
				tradeAdapter = new HistoryAdapter(this, orderModel.order_list,
						4);
				xlistView.setAdapter(tradeAdapter);
			} else {
				tradeAdapter.list = orderModel.order_list;
				tradeAdapter.notifyDataSetChanged();
			}

			tradeAdapter.parentHandler = messageHandler;

		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		Resources resource = (Resources) getBaseContext().getResources();
		xlistView.stopRefresh();
		xlistView.stopLoadMore();
		if (url.endsWith(ProtocolConst.ORDER_LIST)) {
			xlistView.setRefreshTime();
			if (orderModel.paginated.more == 0) {
				xlistView.setPullLoadEnable(false);
			} else {
				xlistView.setPullLoadEnable(true);
			}
			setOrder();
		} else if (url.endsWith(ProtocolConst.ORDER_PAY)) {
			Intent intent = new Intent(this, PayWebActivity.class);
			String data = null;
			try {
				data = jo.getString("data").toString();
				intent.putExtra("html", data);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			startActivity(intent);
		} else if (url.endsWith(ProtocolConst.ORDER_CANCLE)) {
			orderModel.getOrder(flag);
		} else if (url.endsWith(ProtocolConst.AFFIRMRECEIVED)) {

			String suc = resource.getString(R.string.successful_operation);
			String check = resource.getString(R.string.check_or_not);
			mDialog = new MyDialog(this, suc, check);
			mDialog.show();
			mDialog.positive.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mDialog.dismiss();
					Intent intent = new Intent(HistoryActivity.this,
							HistoryActivity.class);
					intent.putExtra("flag", "finished");
					startActivity(intent);
					finish();
				}
			});
			mDialog.negative.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mDialog.dismiss();
				}
			});

			orderModel.getOrder(flag);

		}

	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==0 || resultCode==0){
			orderModel.getOrder(flag);
		}
	}

	@Override
	public void onRefresh(int id) {
		orderModel.getOrder(flag);
	}
	 /** 
     * 得到返回的信息，并处理 
     */  
	@Override
	public void onLoadMore(int id) {
		orderModel.getOrderMore(flag);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (BaolebeiManager.getUmengKey(this) != null) {
			MobclickAgent.onPageStart(getIntent().getStringExtra("flag"));
			MobclickAgent.onResume(this, BaolebeiManager.getUmengKey(this), "");
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (BaolebeiManager.getUmengKey(this) != null) {
			MobclickAgent.onPageEnd(getIntent().getStringExtra("flag"));
			MobclickAgent.onPause(this);
		}
	}

}
